﻿
Partial Class pages_settings_SettingValue
    Inherits CPage

#Region "Querystring"
    Public ReadOnly Property SettingId() As Integer
        Get
            Return CWeb.RequestInt("settingId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Setting]() As SchemaMikeConnellMinistries.CSetting
        Get
            Return CSetting.Cache.GetById(SettingId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.SettingValue(Me.Setting.SettingId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Settings)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        Me.PageTitle = "Setting Value" 'Setting.SettingName

        'Textbox logic
        Me.Form.DefaultButton = btnSave.UniqueID

        AddMenuSide("Settings", CSitemap.Settings())
        AddMenuSide("Setting Details", CSitemap.SettingEdit(SettingId))
        AddMenuSide("Setting Value")

        ddSettings.DataSource = CSetting.Cache
        ddSettings.DataBind()
        CDropdown.SetValue(ddSettings, SettingId)

        fu.FolderPath = CSitemap.SettingUploads

        If CSession.IsDeveloper Then AddButton(CSitemap.SettingAdd(), "Create a new Setting")

        Select Case Setting.TypeId
            Case ESettingType.ProductId
                Dim list As New CProductList()
                list.AddRange(CProduct.Cache.ConferenceSets)
                list.AddRange(CProduct.Cache.Manuals)
                'list.AddRange(CProduct.Cache.Sermons)
                ddProduct.Visible = True
                ddProduct.DataSource = list
                ddProduct.DataBind()
                ddProduct.BlankItem("-- Select a Product --")
                ddProduct.ValueInt = Setting.SettingInteger

                ctrlProduct.Visible = True
                ctrlProduct.Display(Setting.SettingInteger)

            Case ESettingType.Html, ESettingType.Text
                txtBlob.Visible = True
                txtBlob.Text = Setting.SettingString

            Case ESettingType.Boolean
                chk.Visible = True
                chk.Checked = Setting.SettingBoolean
                chk.Text = Setting.SettingName

            Case ESettingType.Integer
                txtInteger.Visible = True
                txtInteger.ValueInt = Setting.SettingInteger

            Case ESettingType.Image
                fu.visible = True
                fu.FileName = Setting.SettingString
        End Select
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

    End Sub
#End Region

    Protected Sub ddSettings_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSettings.SelectedIndexChanged
        Response.Redirect(CSitemap.SettingValue(CDropdown.GetInt(ddSettings)))
    End Sub

    Protected Sub btnSave_Click(sender As Object, e As System.EventArgs) Handles btnSave.Click
        If ddProduct.Visible Then
            Setting.SettingInteger = ddProduct.ValueInt
        ElseIf txtBlob.Visible Then
            Setting.SettingString = txtBlob.Text
        ElseIf chk.Visible Then
            Setting.SettingBoolean = chk.Checked
        ElseIf txtInteger.Visible Then
            Setting.SettingInteger = txtInteger.ValueInt
        ElseIf fu.Visible = True Then
            If fu.HasFile Then
                fu.FolderPath = CSitemap.SettingUploads(False)
                Setting.SettingString = fu.SaveFile()
            End If
        End If
        Setting.Save()
        Refresh()
    End Sub

    Protected Sub btnCancel_Click(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub

    Protected Sub fu_DeleteClick() Handles fu.DeleteClick
        Setting.SettingString = String.Empty
        Setting.Save()
        Response.Redirect(Request.RawUrl)
    End Sub
End Class
